﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class cronjob : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        debioscoopEntities db = new debioscoopEntities();

        DateTime currentDate = DateTime.Today;

        List<int> removeOrderIDs = new List<int>();

        foreach (bestelling order in db.bestellings)
        {
            DateTime orderDateTime = new DateTime(order.datum.Year, order.datum.Month, order.datum.Day, order.tijd.Hours, order.tijd.Minutes, order.tijd.Seconds);

            if (order.soort.Equals("reservering") && !order.afgehaald)
            {
                if (orderDateTime.Year < currentDate.Year || orderDateTime.Month < currentDate.Month || orderDateTime.Day < currentDate.Day || orderDateTime.AddMinutes(30) < DateTime.UtcNow.AddHours(1))
                {
                    removeOrderIDs.Add(order.bestelling_id);
                    db.bestellings.Remove(order);
                }
            }
        }

        foreach (kaartje ticket in db.kaartjes)
        {
            if (removeOrderIDs.Contains(ticket.bestellingbestelling_id))
            {
                db.kaartjes.Remove(ticket);
            }
        }

        db.SaveChanges();

        Response.Redirect(Page.ResolveClientUrl("~/"));
    }
}